﻿module fs.Base
open System
open Option
//open System.IO
//let copy a b = File.Copy(a, b)
//let combine a b = Path.Combine(a, b)
//let create d = Directory.CreateDirectory d |> ignore
//let prepare d = if not (Directory.Exists d) then create d
//
//let find files expr = seq { for f in files do if expr (File.ReadAllText f) then yield f }
//let expr (v: string) = v.Contains "~ST*210*15025~"
//let files = Directory.EnumerateFiles @"F:\Yi\AS2\201104011100, sent"
//let founds = find files expr
//let d = Directory.CreateDirectory "123"
//printf "%s" d.FullName
//let dest p = combine d.FullName (Path.GetFileName p)
//Seq.iter (fun p -> copy p (dest p)) founds
//let equals a b = a.Equals b
//let (?) a b = match a with null -> b | _ -> a
module Type = let none = Unchecked.defaultof<Type>
module String =
    let trim v (a: string) = a.Trim v
    let replace (a: string) (b: string) (v: string) = v.Replace(a, b)
    let toLower (a: obj) =
        match a with
        | :? System.String as a -> a.ToLower()
        | _ -> (string a).ToLower()
//    let bl = String.Empty
    module OrdinalIgnoreCase =
        let startsWith (a: string) b = a.StartsWith(b, StringComparison.OrdinalIgnoreCase)
        let endsWith (a: string) b = a.EndsWith(b, StringComparison.OrdinalIgnoreCase)
        let equals (a: string) b = a.Equals(b, StringComparison.OrdinalIgnoreCase)
    module Coding =
        open System.Text.RegularExpressions
        let pascalToCamel v =
            if v <> "" then (v.Remove 1 |> toLower) + v.Substring 1 else v
        let toTitleize a = Regex.Replace(a, "([A-Z]+)([A-Z][a-z])", "$1 $2") |> (fun a -> Regex.Replace(a, "([a-z\d])([A-Z])", "$1 $2")) |> (fun a -> Regex.Replace(a, "([a-z])(\d)", "$1 $2"))
let br = Environment.NewLine
module Guid =
    let empty = Guid.Empty
    let toString (g: Guid) = g.ToString "N"
let newGuid _ = Guid.NewGuid()
module DateTime =
    let parse v = DateTime.Parse v
let now _ = DateTime.Now
module Option =
    let fromNullable (v : System.Nullable<_>) = if v.HasValue then Some v.Value else None
    let toNullable (v : Option<_>) = if Option.isSome v then Nullable<_>(get v) else Nullable<_>()
    let fromSeq a = Seq.tryFind (fun _ -> true) a
    let toListAsSeq a = Option.toList a |> seq
module Seq =
    open System.Linq
    let ofType<'a> a = Enumerable.OfType<'a> a
    let last = Enumerable.Last
    let reverse = Enumerable.Reverse
//    let ofType<'a> a = a |> Seq.choose (fun a -> match box a with | :? 'a as x -> Some x | _ -> None)//Enumerable.OfType<'a> a
//    let some a = Some a
//    let none = None
module Enum = let parse t v = Enum.Parse (t, v)
module Text =
    open System.Text
    let b (v: string) = StringBuilder v
    let toCamel (v: string) =
        let r = StringBuilder v
        r.[0] <- Char.ToLower v.[0]
        r.ToString()
module Path =
    open System.IO
    let combine a b = Path.Combine (a, b)
    let append b a = combine a b
    let fileName a = Path.GetFileName a
    let extension a = Path.GetExtension a
    let changeExtension x a = Path.ChangeExtension (a, x)
    let directory a = Path.GetDirectoryName a
    let getFileNameWithoutExtension a = Path.GetFileNameWithoutExtension a
module Process =
    open System.Diagnostics
    open System.Security
//        let start path = Process.Start path
    let startWithParam path p = Process.Start (path, p)
    //是系统的 RunAs 服务（Windows 2000 下） / Secondary Logon 服务（Windows XP 下）没有被禁用并处于已启动状态
//    let startAsAdministrator f args =
//        use p = new SecureString()
//        "aa" |> Seq.iter (fun a -> p.AppendChar a)
//        Process.Start (string f, string args, "aa", p, "")
//    let getProcessesByName v = Process.GetProcessesByName(v)

module Directory =
    open System.IO
    let exists p = Directory.Exists p
    let create p = Directory.CreateDirectory p |> ignore
    let getFiles p = Directory.GetFiles p
    let getFilesWithPatern path patern = Directory.GetFiles (path, patern)
    let getAllDescendantsFilesWithPatern path patern = Directory.GetFiles (path, patern, SearchOption.AllDirectories)
module File =
    open System.IO
    let move s d = File.Move (s, d)
    let exists p = File.Exists p
    let delete p = File.Delete p
//        let isWord f = Path.extension f = ".doc"
    module Text =
        let writeAll file text =
            let d = Path.directory file
            if not (Directory.exists d) then Directory.create d
            File.WriteAllText (file, text)
        let writeAllByEncoding file text encoding =
            let d = Path.directory file
            if not (Directory.exists d) then Directory.create d
            File.WriteAllText (file, text, encoding)
        let append f t = File.AppendAllText (f, t)
        let readAll file = File.ReadAllText file
        let readAllLines file = File.ReadAllLines file
//    module Pdf =
////            open System.Drawing.Printing
//        open Microsoft.Office.Interop.Word
////            open Web.HttpContext.Current.Server
//        let printWith file printer = Process.startAsAdministrator @"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe" (sprintf "/t \"%s\" \"%s\"" file printer.Name) |> ignore
module Testing = let browseTo f = Process.startWithParam "explorer.exe" (sprintf "/select, %s" f)